package cn.xm.canting.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.LitePal;

import java.util.List;

import cn.djp.canting.R;
import cn.xm.canting.bean.User;

//登录
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView title;
    private EditText ed_1;
    private EditText ed_2;
    private Button btn_login;
    private TextView register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initView() {
        title = (TextView) findViewById(R.id.title);
        ed_1 = (EditText) findViewById(R.id.ed_1);
        ed_2 = (EditText) findViewById(R.id.ed_2);
        btn_login = (Button) findViewById(R.id.btn_login);
        register = (TextView) findViewById(R.id.register);

        btn_login.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                //点击按钮进行登录验证
                submit();
                break;
        }
    }

    private void submit() {
        User user = new User();
        // validate
        String phone = ed_1.getText().toString().trim();
        //判断用户输入的手机号是否为空
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入账号/手机号~", Toast.LENGTH_SHORT).show();
            return;
        } else {
            //将手机号存入user中
            user.setPhone(phone);
        }

        String password = ed_2.getText().toString().trim();
        //判断用户输入的手机号是否为空
        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码~", Toast.LENGTH_SHORT).show();
            return;
        } else {
            //将密码存入user中
            user.setPassword(password);
        }
        //用Litepal查询User表中的phone列
        List<User> users = LitePal.where("phone = ?", user.getPhone()).find(User.class);

        if (users.size() != 0) {
            //查询到手机号则判断密码是否正确
            if (user.getPassword().equals(users.get(0).getPassword())) {
                Toast.makeText(this, "登录成功~", Toast.LENGTH_SHORT).show();
                //密码一致则实行带数据跳转
                Intent intent = new Intent(LoginActivity.this, OrderActivity.class);
                intent.putExtra("phone", user.getPhone());
                intent.putExtra("password", user.getPassword());
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(this, "密码错误~", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "没有该手机号~", Toast.LENGTH_SHORT).show();
        }
    }
}
